package vashion.azeroth.api.controller.es;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import vashion.azeroth.core.es.pojo.Item;
import vashion.azeroth.core.es.repository.ItemRepository;
import vashion.azeroth.core.item.pojo.ItemDO;
import vashion.azeroth.core.item.pojo.TagDO;
import vashion.azeroth.core.manager.item.BaseItemManager;
import vashion.azeroth.core.manager.item.TagManager;
import vashion.azeroth.core.result.ApiResult;
import vashion.azeroth.core.service.es.ItemESService;
import vashion.azeroth.core.service.es.query.ItemQuery;

@Controller
@RequestMapping("/es/item")
public class ItemEsController<T> {

	private final static Logger log = LoggerFactory.getLogger(ItemEsController.class);

	@Autowired
	private ItemRepository itemRepository;
	@Autowired
	private ItemESService itemESService;
	@Autowired
	private BaseItemManager baseItemManager;
	@Autowired
	private TagManager tagManager;

	@RequestMapping("init.htm")
	@ResponseBody
	public ApiResult<String> init() {

		ApiResult<String> result = new ApiResult<String>();
		
		List<ItemDO> items = baseItemManager.findAll(new ItemDO());
		
		for (ItemDO itemDO : items) {
			log.info(String.valueOf(itemDO.getId()));
			
			Item item = new Item();
			item.setId(itemDO.getId());
			item.setBrandTitle(itemDO.getBrandTitle());
			item.setItemDesc(itemDO.getItemDesc());
			item.setPic(itemDO.getPic());
			item.setPriceDisplay(itemDO.getPriceDisplay());
			
			List<TagDO> tags = tagManager.findByItemId(itemDO.getId());
			
			if(tags != null) {
				String tag = "";
				for (TagDO tagDO : tags) {
					tag += tagDO.getTitle() + " ";
				}
				item.setTag(tag);
			}
			
			itemRepository.save(item);
		}
		
		return result;
	}
	
	@RequestMapping("initone.htm")
	@ResponseBody
	public ApiResult<String> initone() {

		ApiResult<String> result = new ApiResult<String>();
		
		ItemDO itemDO = baseItemManager.get(19305L);
		
		log.info(String.valueOf(itemDO.getId()));
		
		Item item = new Item();
		item.setId(itemDO.getId());
		item.setBrandTitle(itemDO.getBrandTitle());
		item.setItemDesc(itemDO.getItemDesc());
		item.setPic(itemDO.getPic());
		item.setPriceDisplay(itemDO.getPriceDisplay());
		
		List<TagDO> tags = tagManager.findByItemId(itemDO.getId());
		
		if(tags != null) {
			String tag = "";
			for (TagDO tagDO : tags) {
				tag += tagDO.getTitle() + " ";
			}
			item.setTag(tag);
		}
		
		itemRepository.save(item);
		
		return result;
	}
	
    @RequestMapping("search.htm")
    @ResponseBody
    public ApiResult<List<Item>> searchItem(ItemQuery itemQuery) {
    	
    	ApiResult<List<Item>> result = new ApiResult<List<Item>>();
    	
    	Page<Item> itemPage = itemESService.searchItem(itemQuery);
    	
    	result.setData(itemPage.getContent());
    	result.setTotalEsCount(itemPage.getTotalElements());
        return result;
    }

    @RequestMapping("searchmItem.htm")
    @ResponseBody
    public ApiResult<List<Item>> searchMutiItem(ItemQuery itemQuery) {
    	ApiResult<List<Item>> result = new ApiResult<List<Item>>();
    	Page<Item> itemPage = itemESService.searchMutiItem(itemQuery);
    	result.setData(itemPage.getContent());
    	result.setTotalEsCount(itemPage.getTotalElements());
        return result;
    }

}
